<template>
  <div class="outMore">
    <el-dialog
      title="提示"
      :visible.sync="dialogAllDisable"
      width="540px"
      class="safeDisableDialog"
      style="margin-top: 20vh;">
      <div style="width: 100%; text-align: center; height: 100px; padding-top: 35px;">
        <img :src="disableIcon"/>
      </div>
      <span>确认批量禁用联动</span>
      <span slot="footer" class="dialog-footer">
          <el-button class="clears" @click="dialogAllDisable = false">取 消</el-button>
          <el-button type="primary" style="float: right" @click="disableAllWork()">确 定</el-button>
        </span>
    </el-dialog>
    <el-dialog
      title="提示"
      :visible.sync="dialogAllEnable"
      width="540px"
      class="safeDisableDialog"
      style="margin-top: 20vh;">
      <div style="width: 100%; text-align: center; height: 100px; padding-top: 35px;">
        <img :src="enableIcon"/>
      </div>
      <span>确认批量启用联动</span>
      <span slot="footer" class="dialog-footer">
          <el-button class="clears" @click="dialogAllEnable = false">取 消</el-button>
          <el-button type="primary" style="float: right" @click="enableAllWork()">确 定</el-button>
        </span>
    </el-dialog>
    <el-dialog class="dialogSafeAdd" title="新增联动" :visible.sync="dialogSafeAdd">
      <el-form ref="form" label-width="80px">
        <span id="addNameSpan">*</span>
        <el-form-item class="editForm" label="策略名称:">
          <el-input class="editInput" v-model="addlinkageName"></el-input>
        </el-form-item>
        <el-row class="editRow">
          <el-col class="editRowLeft" :span="8">
            <el-input class="leftInput" v-model="addLinkageNeedName" placeholder="输入关键字进行过滤"></el-input>
            <div class="grid-content bg-purple treeFather">
              <el-tree
                :data="linkageTreeData"
                :props="linkageTreeprops"
                node-key="resource_id"
                ref="tree"
                :highlight-current="true"
                :filter-node-method="addLinkagefilterNode"
                @node-click="addLinkageNodeClick">
              </el-tree>
            </div>
          </el-col>
          <el-col class="linkageRowRight" :span="16">
            <div>
              <el-form class="linkageForm" :rules="rules" :model="addlinkage" ref="form" :hide-required-asterisk="true" label-width="82px">
                <el-form-item class="linkageLabel" prop="Condition" label="触发条件:">
                  <el-select class="linkageSelect" v-model="addlinkage.Condition" size="small" multiple placeholder="请选择">
                    <el-option
                      v-for="item in linkageTermsData"
                      :key="item.value"
                      :label="item.label"
                      :value="item.value">
                    </el-option>
                  </el-select>
                </el-form-item>
                <div class="formsBackground">
                  <el-form-item class="linkageLabel" prop="Action" label="触发动作:">
                    <el-select class="linkageSelect" v-model="addlinkage.Action" placeholder="请选择">
                      <el-option
                        v-for="item in linkageTypeData"
                        :key="item.value"
                        :label="item.label"
                        :value="item.value">
                      </el-option>
                    </el-select>
                  </el-form-item>
                  <div>
                    <el-table class="linkageTable" :data="addlinkageVideo">
                      <el-table-column class="tablePadding" width="395px" prop="name">
                        <template slot-scope="scope">
                          <div class="videoBlock">
                            <span class="videoSpan">*</span>
                            <span class="demonstration">视频参数{{scope.$index + 1}}:</span>
                            <el-input class="videoInput" v-model="addlinkageVideo[scope.$index]" placeholder="格式: ip地址#端口#用户名#密码#通道号"></el-input>
                          </div>
                          <!--<el-input v-model="addlinkageVideo[scope.$index].name"></el-input>-->
                        </template>
                      </el-table-column>
                      <el-table-column class="tablePadding">
                        <template  slot-scope="scope">
                          <el-button v-if="scope.$index < 8" class="tableIcon" title="添加" type="text" size="small" @click="addAddTime(scope.$index)"><i class="el-icon-plus"></i></el-button>
                          <el-button v-if="scope.$index > 0" class="tableIcon" title="删除" type="text" size="small" @click="addDeleteRow(scope.$index)"><i class="el-icon-minus"></i></el-button>
                        </template>
                      </el-table-column>
                    </el-table>
                  </div>
                  <el-form-item class="linkageLabel" prop="Photograph" label="拍照张数:">
                    <el-input v-model="addlinkage.Photograph"></el-input>
                  </el-form-item>
                  <el-form-item class="linkageLabel" prop="Timeinterval" label="时间间隔:">
                    <el-input class="interval" v-model="addlinkage.Timeinterval"></el-input><span class="numberOfSeconds">s</span>
                  </el-form-item>
                </div>
                <el-form-item class="linkageLabel" prop="EffectTime" label="生效时间:">
                  <el-time-picker
                    class="forceTime"
                    is-range
                    v-model="addlinkage.EffectTime"
                    size="small"
                    range-separator="至"
                    format="HH:mm:ss"
                    value-format="HH:mm:ss"
                    start-placeholder="开始日期"
                    end-placeholder="结束日期"
                    placeholder="选择时间范围">
                  </el-time-picker>
                </el-form-item>
              </el-form>
            </div>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <div class="notes">注: <span id="editAsterisk">*</span> 为必填项</div>
        <el-button class="clears" @click="dialogSafeAdd = false">取 消</el-button>
        <el-button class="bottonConfirm" type="primary" @click="addLinkage('form')">确 定</el-button>
      </div>
    </el-dialog>
    <el-form :inline="true" class="demo-form-inline">
      <el-form-item class="handItem" label="联动策略列表">
        <el-input class="handInput" v-model="linkageId" ></el-input><i @click="searchLinkage" class="el-icon-search  search"></i>
        <el-button v-show="returnShow" class="indieWidth Batchdisable" type="primary" @click="outlineLinkage">
          重置
        </el-button>
      </el-form-item>
      <el-form-item class="handItem" style="float: right">
        <el-button class="indieWidth" type="primary" @click="addDialog()">
          <img :src="addsite"/>新增联动
        </el-button>
        <el-button type="primary" class="clear" @click="linkageDeleteDialog">
          <img :src="whitedelete"/>批量删除
        </el-button>
        <el-button class="indieWidth" type="primary" @click="linkageEnable">
          <img :src="whiteenable"/>批量启用
        </el-button>
        <el-button class="indieWidth Batchdisable" type="primary" @click="linkageDisable">
          <img :src="whitedisable"/>批量禁用
        </el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
  import addsite from '@/assets/mSiteAdmin/add.png'
  import whitedelete from '@/assets/mSiteAdmin/whitedelete.png'
  import whiteenable from '@/assets/mSiteAdmin/whiteenable.png'
  import whitedisable from '@/assets/mSiteAdmin/whitedisable.png'
  import enableIcon from '@/assets/mSiteAdmin/enable-icon.png'
  import disableIcon from '@/assets/mSiteAdmin/disable-icon.png'
  import axios from 'axios'
  export default {
    props: ['deleteids'],
    data() {
      return {
        addsite,
        whitedelete,
        whiteenable,
        whitedisable,
        enableIcon,
        disableIcon,
        dialogAllDisable: false,
        dialogAllEnable: false,
        rules: {
          Condition: [
            { type: 'array', required: true, message: ' ', trigger: 'change' }
          ],
          Action: [
            { required: true, message: ' ', trigger: 'change' }
          ],
          Photograph: [
            { required: true, message: ' ', trigger: 'blur' }
          ],
          Timeinterval: [
            { required: true, message: ' ', trigger: 'blur' }
          ],
          EffectTime: [
            { required: true, message: ' ', trigger: 'change' }
          ]
        },
        addlinkage: {
          Condition: [],
          Action: '',
          Photograph: '',
          Timeinterval: '',
          EffectTime: ''
        },
        dialogSafeAdd: false,
        addlinkageVideo: [''],
        addlinkageCondition: [],
        addlinkageAction: '',
        addlinkageName: '',
        addlinkagePhotograph: '',
        addlinkageTimeinterval: '',
        addlinkageEffectTime: '',
        addLinkageNeedName: '',
        addsafe: [{
          name: ''
        }],
        linkageId: '',
        linkageTypeData: [],
        linkageTermsData: [],
        count: '1',
        form: {
          name: ''
        },
        linkageTreeprops: {
          label: 'name'
        },
        returnShow: false,
        highlightcurrent: true,
        site: {
          id: ''
        },
        linkageTreeData: [],
        // 新增请求体
        linkageAdd: {
          access_token: localStorage.getItem('access_token'),
          type: 'strategy_add',
          pid: localStorage.getItem('num'),
          data: {
            name: '', // 测点位置名字
            rid: '', // 编号
            terms: [], // 触发条件(多选)
            linkage_val: '', // 触发动作编号
            linkage_type: '', // 触发动作
            addrs: [], // 视频参数
            photo_num: '', // 拍照张数
            interval: '', // 时间间隔
            valid_begin_time: '', // 生效开始
            valid_end_time: '', // 生效结束
            status: 1 // 启用状态
          }
        },
        // 删除请求体
        linkageDelete: {
          access_token: localStorage.getItem('access_token'),
          type: 'strategy_del',
          pid: localStorage.getItem('num'),
          ids: []
        },
        // 获取触发条件
        linkageTermsGet: {
          access_token: localStorage.getItem('access_token'),
          type: 'strategy_get_terms',
          pid: localStorage.getItem('num'),
          id: '',
          ids: []
        },
        // 获取触发动作
        linkageTypeGet: {
          access_token: localStorage.getItem('access_token'),
          type: 'strategy_get_type',
          pid: localStorage.getItem('num')
        },
        // 获取组织树
        linkageTree: {
          access_token: localStorage.getItem('access_token'),
          type: 'resource_getall',
          pid: localStorage.getItem('num'),
          ids: ['']
        },
        // 启用安全时段
        linkageEnableBody: {
          access_token: localStorage.getItem('access_token'),
          type: 'strategy_enable',
          pid: localStorage.getItem('num'),
          ids: []
        },
        // 禁用安全时段
        linkageDisableBody: {
          access_token: localStorage.getItem('access_token'),
          type: 'strategy_disable',
          pid: localStorage.getItem('num'),
          ids: []
        }
      }
    },
    watch: {
      addLinkageNeedName(val) {
        this.$refs.tree.filter(val)
      }
    },
    methods: {
      addLinkagefilterNode(value, data) {
        if (!value) return true
        return data.name.indexOf(value) !== -1
      },
      // 新增操作
      addLinkage(form) {
        if (!this.addlinkageName) {
          this.$message.error('请输入策略名称')
        } else {
          if (!this.linkageTermsGet.id) {
            this.$message.error('请勾选测点树')
          } else {
            if (this.addlinkage.Condition.length === 0) {
              this.$message.error('请选择触发条件')
            } else {
              if (!this.addlinkage.Action) {
                this.$message.error('请选择触发动作')
              } else {
                if (!this.addlinkage.Photograph) {
                  this.$message.error('请输入拍照张数')
                } else {
                  if (!this.addlinkage.Timeinterval) {
                    this.$message.error('请输入时间间隔')
                  } else {
                    if (this.addlinkage.EffectTime.length === 0) {
                      this.$message.error('请选择生效时间')
                    }
                  }
                }
              }
            }
          }
        }
        this.linkageAdd.data.name = this.addlinkageName
        this.linkageAdd.data.rid = this.linkageTermsGet.id
        this.linkageAdd.data.terms = this.addlinkage.Condition
        this.linkageAdd.data.linkage_val = this.addlinkage.Action
        this.linkageAdd.data.linkage_type = this.addlinkage.Action
        this.linkageAdd.data.addrs = this.addlinkageVideo
        this.linkageAdd.data.photo_num = this.addlinkage.Photograph
        this.linkageAdd.data.interval = this.addlinkage.Timeinterval
        this.linkageAdd.data.valid_begin_time = this.addlinkage.EffectTime[0]
        this.linkageAdd.data.valid_end_time = this.addlinkage.EffectTime[1]
        axios.post('/linkagemanage', this.linkageAdd, {
          headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
          }
        }).then((res) => {
          if (res.data.errcode === '00000') {
            this.$refs.tree.setCheckedKeys([''])
            this.$emit('findLinkageId', this.linkageId)
            this.dialogSafeAdd = false
          } else {
            this.$message.error(res.data.errmsg)
          }
        })
          .catch((error) => {
            this.$message.error(error.data.errmsg)
          })
      },
      // 新增弹框
      addDialog() {
        this.dialogSafeAdd = true
        this.addsafe.id = ''
        this.linkageTreeData = []
        this.addlinkageVideo = []
        this.addlinkage.Condition = []
        this.addlinkage.Action = ''
        this.addlinkageName = ''
        this.addlinkage.Photograph = ''
        this.addlinkage.Timeinterval = ''
        this.addlinkage.EffectTime = ''
        this.addAddTime()
        axios.post('/linkagemanage', this.linkageTypeGet, {
          headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
          }
        }).then((res) => {
          this.linkageTypeData = []
          this.linkageTypeData = res.data.datas
        })
          .catch(function(error) {
            console.log(error)
          })
        axios.post('/project', this.linkageTree, {
          headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
          }
        }).then((res) => {
          this.linkageTreeData.push(res.data.datas)
        })
          .catch((error) => {
            this.$message.error(error.data.errmsg)
          })
      },
      // 增加时段
      addAddTime() {
        if (this.addlinkageVideo.length < 9) {
          this.addlinkageVideo.push('')
        }
      },
      // 减少时段
      addDeleteRow(index) {
        if (this.addlinkageVideo.length > 1) {
          this.addlinkageVideo.splice(index, 1)
        }
      },
      // 树的点击操作
      addLinkageNodeClick(data) {
        if (data.resource_id) {
          this.linkageTermsGet.id = ''
          this.linkageTermsGet.id = data.resource_id
          axios.post('/linkagemanage', this.linkageTermsGet, {
            headers: {
              'Content-Type': 'application/x-www-form-urlencoded'
            }
          }).then((res) => {
            this.linkageTermsData = []
            this.linkageTermsData = res.data.datas
          })
            .catch(function(error) {
              console.log(error)
            })
        }
      },
      // 查询事件
      searchLinkage() {
        if (this.linkageId === '') {
          this.$message({
            message: '请输入策略名称!',
            type: 'error'
          })
        } else {
          this.returnShow = true
          this.$emit('findLinkageId', this.linkageId)
        }
      },
      // 重置查询
      outlineLinkage() {
        this.returnShow = false
        this.linkageId = ''
        this.$emit('findLinkageId', this.linkageId)
      },
      // 删除按钮事件
      linkageDeleteDialog() {
        if (this.deleteids.length === 0) {
          this.$message({
            showClose: true,
            message: '请勾选要批量删除的策略！',
            type: 'error'
          })
        } else {
          this.$confirm('确认批量删除策略？', '提示', {
            cancelButtonClass: 'userCancel',
            confirmButtonClass: 'userConfirm',
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {
            this.linkageBatchDelete()
          }).catch(() => {
          })
        }
      },
      // 批量删除操作
      linkageBatchDelete() {
        this.linkageDelete.ids = this.deleteids
        axios.post('/linkagemanage', this.linkageDelete, {
          headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
          }
        }).then((res) => {
          this.linkageDelete.ids = []
          this.$message({
            type: 'success',
            message: '批量删除成功'
          })
          this.$emit('findLinkageId', this.linkageId)
        })
          .catch(function(error) {
            console.log(error)
          })
      },
      // 启用
      linkageEnable(index, row) {
        if (this.deleteids.length === 0) {
          this.$message({
            showClose: true,
            message: '请勾选要批量启用的策略！',
            type: 'error'
          })
        } else {
          this.dialogAllEnable = true
          this.linkageEnableBody.ids = this.deleteids
        }
      },
      enableAllWork() {
        axios.post('/linkagemanage', this.linkageEnableBody, {
          headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
          }
        }).then((res) => {
          this.dialogAllEnable = false
          this.$message({
            type: 'success',
            message: '批量启用成功'
          })
          this.linkageEnableBody.ids = []
          this.$emit('findLinkageId', this.linkageId)
        })
          .catch(function(error) {
            console.log(error)
          })
      },
      // 禁用
      linkageDisable(index, row) {
        if (this.deleteids.length === 0) {
          this.$message({
            showClose: true,
            message: '请勾选要批量禁用的策略！',
            type: 'error'
          })
        } else {
          this.dialogAllDisable = true
          this.linkageDisableBody.ids = this.deleteids
        }
      },
      disableAllWork() {
        axios.post('/linkagemanage', this.linkageDisableBody, {
          headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
          }
        }).then((res) => {
          this.dialogAllDisable = false
          this.$message({
            type: 'success',
            message: '批量禁用成功'
          })
          this.linkageDisableBody.ids = []
          this.$emit('findLinkageId', this.linkageId)
        })
          .catch(function(error) {
            console.log(error)
          })
      }
    }
  }
</script>

<style scoped>
  .outMore{
    width: 100%;
    height: 50px;
    background-color: #ffffff;
    padding-top: 5px;
    padding-left: 20px;
    border-bottom: 1px solid #e0e0e0;
  }
  /deep/ .search{
    position: absolute;
    margin-top: 13px;
    margin-left: -30px;
    cursor: pointer;
    color: #0072F3;
  }
  .Batchdisable{
    background-color: #D05133 !important;
    border: 1px solid  #D05133 !important;
  }
  /deep/ label {
    font-weight: normal;
    color: #69727E;
  }
  /deep/ .handItem {
    margin-bottom: 5px!important;
  }
  /deep/ .indieWidth {
    padding: 6px 8px;
  }
  /deep/ .addAllSpan {
    float: left;
    margin: 10px 0px 0px 5px;
    color: #E53D41;
  }
  /deep/ .reset {
    padding: 6px 16px;
    background-color: #333B3D;
    border-color: #333B3D ;
    color: #fff;
  }
  /deep/ .el-button + .el-button {
    margin-left: 5px;
  }
  /deep/ .el-button--primary {
    height: 30px;
    font-size: 14px;
    padding: 6px 16px;
    background: #0072F3;
    border-color: #0072F3;
  }
  /deep/ .handInput {
    width: 365px;
  }
  /deep/ .handInput > .el-input__inner {
    height: 30px;
    line-height: 30px;
    width: 360px;
  }
  /deep/ .clear{
    padding: 6px 16px;
    background-color: #526D89;
    border-color: #526D89 ;
    color: #fff;
    line-height: 0px;
  }
  /deep/ .export {
    background: #fff;
    border: 1px solid #0072F3;
    color: #0072F3;
  }
  /deep/ .el-icon-delete {
    height: 16px;
    line-height: 12px;
  }
  /deep/ .el-icon-delete:before {
    font-size: 16px;
    color: #fff;
  }
  /deep/ .el-message-box{
    width: 540px;
  }
  /deep/ .el-message-box__header{
    background-color: #E3E6EE;
    padding: 10px 15px;
  }
  /deep/ .el-message-box__title{
    font-size: 18px;
    font-weight: Bold;
    color: #323842;
  }
  /deep/ .el-message-box__status{
    right: 247px;
    top: 65px;
    font-size: 48px !important;
  }
  /deep/ .el-message-box__status.el-icon-warning{
    color: #E53D41;
  }
  /deep/ .el-message-box__content{
    height: 182px;
  }
  /deep/ .el-message-box__message p {
    margin: 0;
    font-size: 18px;
    color: #323842;
    padding-top: 96px;
    text-indent: 0px;
    text-align: center;
  }
  /deep/ .el-message-box__headerbtn{
    top: 9px;
    font-size: 18px;
  }
  /deep/ .el-message-box__btns{
    padding: 10px 15px 7px ;
    border-top: 1px solid #e8eaf1;
  }
  /deep/ .dialogSafeAdd > .el-dialog > .el-dialog__body {
    padding: 0px;
  }
  /deep/ .dialogSafeAdd > .el-dialog > .el-dialog__footer {
    height: 50px;
  }
  /deep/ .formsBackground > .el-input__inner {
    width:100%;
  }
  /deep/ .el-dialog {
    width: 800px;
  }
  /deep/ .editForm {
    padding: 10px 20px;
    margin: 0px;
  }
  /deep/ .editRowLeft {
    background: #EAEDF1;
    border: 1px solid #E4E5E7;
    height: 499px;
  }
  /deep/ .treeFather {
    overflow: auto;
    height: 447px;
  }
  /deep/ .notes {
    float: left;
    height: 30px;
    line-height: 30px;
    font-size: 14px;
    color: #69727E;
  }
  /deep/ #editAsterisk {
    color: #E53D41;
  }
  /deep/ .clears{
    padding: 6px 16px;
    height:30px;
    background-color: #526D89;
    border-color: #526D89 ;
    color: #fff;
    float: right;
    margin-left: 10px;
  }
  /deep/ .bottonConfirm {
    padding: 6px 16px;
    float: right;
    border:1px solid #0072F3;
    background: #0072F3;
  }
  /deep/ .leftInput > .el-input__inner {
    height: 30px;
    margin:10px 20px;
    width: 220px;
  }
  .editSearch{
    position: absolute;
    margin-top: 18px;
    margin-left: -45px;
    cursor: pointer;
    color: #0072F3;
  }
  /deep/ .addEditIcon {
    color: #0072F3;
    font-size: 16px;
    cursor: pointer;
  }
  /deep/ .linkageRowRight {
    padding: 10px;
  }
  /deep/ .linkageForm {
    padding: 10px;
    max-height: 475px;
    overflow-y: auto;
  }
  /deep/ .linkageLabel {
    height: 30px;
    font-weight: normal;
  }
  /deep/ .linkageLabel > .el-form-item__label {
    font-weight: normal;
  }
  /deep/ .linkageSelect {
    width: 100%;
  }
  /deep/ .linkageTable > .el-table__header-wrapper{
    height: 0px;
  }
  /deep/ .linkageTable > .el-table__body-wrapper > .el-table__body > tbody > .el-table__row{
    background: #F0F2F7;
  }
  /deep/ .addSafeTable {
    height: 495px;
    overflow-y: auto;
  }
  /deep/ .addSafeTable > .el-table__header-wrapper{
    height: 0px;
  }
  /deep/ .addSafeTable > .el-table__body-wrapper > .el-table__body > tbody > .el-table__row > .el-table td, .el-table th{
    padding: 5px 0px;
  }
  /deep/ .el-icon-minus {
    color: #D05133;
  }
  /deep/ .timeInterval {
    height: 30px;
    width: 365px;
    float: right;
  }
  /deep/ .formsBackground {
    background: #F0F2F7;
    padding-bottom: 1px;
  }
  /deep/ .el-tree--highlight-current .el-tree-node.is-current > .el-tree-node__content {
    background-color: transparent;
    color: #0072F3;
  }
  #addNameSpan {
    float: left;
    margin: 20px 0px 0px 14px;
    color: #E53D41;
  }
  /deep/ .videoSpan {
    float: left;
    margin-top: 5px;
    color: #E53D41;
  }
  /deep/ .videoBlock {
    margin-left: -5px;
  }
  /deep/ .linkageSpan {
    float: left;
    margin: 10px 0px 0px 0px;
    color: #E53D41;
  }
  /deep/ .safeDisableDialog > .el-dialog > .el-dialog__body {
    text-align: center;
    font-size: 18px;
    height: 180px;
    border-bottom: 1px solid #e8eaf1;
  }
  /deep/ .safeDisableDialog > .el-dialog > .el-dialog__footer {
    height: 60px;
  }
  /deep/ .safeDisableDialog > .el-dialog > .el-dialog__footer > .dialog-footer > button {
    height: 30px;
    font-size: 14px;
    padding: 6px 16px;
  }
</style>
